if not Entity then
	Entity = {}
end

local ec =
{
	prop_vit = 0,
	prop_str = 1,
	prop_agi = 2,
	prop_int = 3,
	prop_left = 4,
	prop_tohit = 5,
	prop_tododge = 6,
	prop_critical = 7,
	prop_resist = 8,
	prop_ap = 9,
	prop_dp = 10,
	prop_max,
}

local initEntity = function(entity, heroc, equip)
end

function Entity.onLoad(entity, equip)
end

function Entity.onLoadP(entity)
	log.info("loading entity player, doing nothing now!")
	--Entity.onLoad(entity, 1)
	--Player don't need to be loaded
end


function Entity.onLoadR(entity)
	log.info("loading entity role!")
	--
	--	default no equipment
	equip = equip or 1
	local lvl = entity:getLevel()
	local heroc = heros[entity:getStaticID()]
	if heroc then
		local cbt = entity:getCombatSys()
		if cbt then
			local vit = heroc.vit.b + heroc.vit.a * lvl * equip
			local str = heroc.str.b + heroc.str.a * lvl * equip
			local agi = heroc.agi.b + heroc.agi.a * lvl * equip
			local int = heroc.int.b + heroc.int.a * lvl * equip

			cbt:setProps(ec.prop_vit, vit)
			cbt:setProps(ec.prop_str, str)
			cbt:setProps(ec.prop_agi, agi)
			cbt:setProps(ec.prop_int, int)

			local hp = vit * 10
			cbt:setMaxHP(hp)
			local mp = int * 10
			cbt:setMaxMP(mp)

			cbt:setProps(ec.prop_tohit, heroc.tohit.b + heroc.tohit.a * agi)
			cbt:setProps(ec.prop_tododge, heroc.tododge.b + heroc.tododge.a * agi)

			cbt:setProps(ec.prop_critical, heroc.c.b + heroc.c.a * lvl)
			cbt:setProps(ec.prop_resist,	heroc.t.b + heroc.t.a * lvl )

			local matt = 0
			if (heroc.cls==1) then
				matt = str
			elseif (heroc.cls==2) then
				matt = agi
			else
				matt = int
			end

			--	ap is base on main attributes
			--	dp is base on str
			cbt:setProps(ec.prop_ap, heroc.ap.b + heroc.ap.a * matt)
			cbt:setProps(ec.prop_dp, heroc.dp.b + heroc.dp.a * str)
		else
			log.error("Failed to init combat system for entity: " , entity:getStaticID())
		end
	else
		log.error("Failed to load role invalid hero id:",entity:getStaticID()) 
	end
	print("############################################")
	entity:dump()
end


function Entity.onLoadM(entity)
	log.info("loading entity monster!")
	local mc = monsters[entity:getStaticID()]
	if not mc then
		log.error("failed to load monster, invalid monster id")
	end

	local lvl = mc.lvl
	entity:setLevel(lvl)
	local equip = mc.equip or 1

	local heroc = heros[mc.hero]
	if heroc then
		local cbt = entity:getCombatSys()
		if cbt then
			local vit = heroc.vit.b + heroc.vit.a * lvl * equip
			local str = heroc.str.b + heroc.str.a * lvl * equip
			local agi = heroc.agi.b + heroc.agi.a * lvl * equip
			local int = heroc.int.b + heroc.int.a * lvl * equip

			cbt:setProps(ec.prop_vit, vit)
			cbt:setProps(ec.prop_str, str)
			cbt:setProps(ec.prop_agi, agi)
			cbt:setProps(ec.prop_int, int)

			local hp = vit * 10 * mc.hp_rate
			cbt:setHP(hp)
			cbt:setMaxHP(hp)
			local mp = int * 10 * mc.mp_rate
			cbt:setMP(mp)
			cbt:setMaxMP(mp)

			cbt:setProps(ec.prop_tohit, heroc.tohit.b + heroc.tohit.a * agi)
			cbt:setProps(ec.prop_tododge, heroc.tododge.b + heroc.tododge.a * agi)

			cbt:setProps(ec.prop_critical, heroc.c.b + heroc.c.a * lvl)
			cbt:setProps(ec.prop_resist, heroc.t.b + heroc.t.a * lvl)

			local matt = 0
			if (heroc.cls==1) then
				matt = str
			elseif (heroc.cls==2) then
				matt = agi
			else
				matt = int
			end

			--	ap is base on main attributes
			--	dp is base on str
			cbt:setProps(ec.prop_ap, heroc.ap.b + heroc.ap.a * matt)
			cbt:setProps(ec.prop_dp, heroc.dp.b + heroc.dp.a * str)
		else
			log.error("Failed to init combat system for entity: " , entity:getStaticID())
		end
	else
		log.error("Failed to load role invalid hero id:",entity:getStaticID()) 
	end
	print("############################################")
	entity:dump()

end
